﻿using LDM.Business.Repository.Repository.Interfaces;
using LDM.Business.ServiceAPI.Model.DBModel;
using LDM.Framework.Caches;
using Microsoft.Extensions.Configuration;
using Model.Enum.ConfigModel;
using Utility.DB.SqlSugar;
using Utility.Extensions;

namespace LDM.Business.Repository.Repository
{
    public class SystemConfigRepository : DbContextBase<SystemConfigPO>, ISystemConfigRepository
    {
        private readonly IMemoryCache _memoryCache;

        public SystemConfigRepository(IConfiguration configuration, IMemoryCache memoryCache)
            : base(configuration.GetConnectionString(ConnectionStringType.OrderMysql.GetDescription()))
        {
            _memoryCache = memoryCache;
        }

        /// <summary>
        /// 获取配置
        /// </summary>
        /// <returns></returns>
        public SystemConfig GetSystemConfig()
        {
            var key = "SystemConfig";
            return _memoryCache.GetFromCacheFirst(key, () =>
            {
                var allConfigs = Db.Queryable<SystemConfigPO>().ToList();
                if (allConfigs == null || allConfigs.Count == 0)
                    return new SystemConfig();
                var config = allConfigs.BuildObjectFromRow<SystemConfig, SystemConfigPO>("ConfigName", "ConfigValue");
                return config;
            }, 60);
        }
    }
}
